/**
 * Created by YAAP on 2017/8/1.
 */
define(['api','AdministrativeDivisionOption'], (api,AdministrativeDivisionOption) => {
    var template =
        `<div class="houseWrapTab1">
                                    <div class="houseTabBox">
                                        <ul class="houseTab1 clearfix">
                                            <li class="inputList">
                                                <h3>小区名称</h3>
                                                <input type="hidden" :value="realEstate.Id"/>
                                                <input   placeholder="name" type="text" v-model="realEstate.Name"/> 

                                                <h3>拼音缩写</h3>
                                                <input type="text"    v-model="realEstate.Pinyin"/>
                                            </li>
                                            <li>
                                                <h3>行政区域</h3>
                                                <strong>
                                                    <Cascader :data="cityData"  v-on:on-change="division_changed"  v-model="realEstate.AddressAdministrativeDivisionCode" ></Cascader>
                                                </strong>

                                                <h3>所属片区</h3>
                                                <strong>
                                                    <i-select v-model="realEstate.CustomAreaId"  >
                                                        <i-option v-for="item in customArea" :value="item.value">
                                                            {{item.label}}
                                                        </i-option>
                                                    </i-select>
                                                    
                                                </strong>
                                            </li>
                                            <li class="inputList">
                                                <h3>概要地址</h3>
                                                <input type="text"   v-model="realEstate.AddressSummary"/>

                                                <h3>小区别名</h3>
                                                <input type="text"   v-model="realEstate.BlockModel.Alias"/>
                                            </li>
                                            <li class="inputListLong">
                                                <h3>详细地址</h3>
                                                <input type="text"   v-model="realEstate.AddressDetail"/>
                                            </li>
                                            <li class="selectList">
                                                <h3>物业类型</h3>
                                                <strong>
                                                    <i-select v-model="realEstate.PropertyTypeValue" >
                                                        <i-option v-for="item in propertyType" :value="item.value">
                                                            {{item.label}}
                                                        </i-option>
                                                    </i-select>
                                                </strong>

                                                <h3>物业用途</h3>
                                                <strong>
                                                    <i-select v-model="realEstate.BlockModel.PropertyUsageValue"  >
                                                        <i-option v-for="item in propertyUsage" :value="item.value">
                                                            {{item.label}}
                                                        </i-option>
                                                    </i-select>
                                                </strong>
                                            </li>
                                            <li class="dateList">
                                                <h3>建房时间</h3>
                                                <strong>
                                                    <Row>
                                                        <Col span="12">
 
                                                        <Date-picker type="date" v-on:on-change="builttime_changed" v-bind:editable="false" 
                                                        v-bind:value="realEstate.BlockModel.BuiltTime"></Date-picker>
 
                                                        </Col>
                                                    </Row>
                                                </strong>

                                                <h3>交房时间</h3>
                                                <strong>
                                                    <Row>
                                                        <Col span="12">
 
                                                        <Date-picker type="date" format="yyyy-MM-dd" v-on:on-change="submittime_changed" 
                                                        v-bind:editable="false" v-bind:value="realEstate.BlockModel.SubmitTime"></Date-picker>
 
                                                        </Col>
                                                    </Row>
                                                </strong>
                                            </li>
                                            <li class="inputList">
                                                <h3>售均价</h3>
                                                <input type="text"  v-model="realEstate.BlockModel.AveragePrice.Price.Amount"/>

                                                <h3>物业费</h3>
                                                <input type="text" v-model="realEstate.BlockModel.PropertyFee"/>
                                            </li>
                                            <li class="txta">
                                                <h3>备注</h3>
                                                <textarea v-model="realEstate.Memo"></textarea>
                                            </li>
                                        </ul>
                                        <!--
                                        <ul class="houseBtmTab1 clearfix">
                                            <li class="houseRule">
                                                <h3>房号规则</h3>
                                                <strong>

                                                </strong>

                                                <h3 class="short">+</h3>
                                                <strong>

                                                </strong>

                                                <h3 class="short">+</h3>
                                                <input type="text" class="inputLong" placeholder="例："/>
                                            </li>
                                            <li class="houseExp">
                                                <h3>房号例外</h3>
                                                <input type="text" v-model="realEstate.BlockModel.roomNoExcudeRule"/>

                                                <h3 class="short">=</h3>
                                                <input type="text"/>

                                                <h3 class="short">+</h3>
                                                <input type="text" class="inputLong" placeholder="例："/>
                                            </li>
                                            <li class="inputListLong checkList">
                                                <h3>楼栋必选</h3>
                                                <h4>
                                                    <Checkbox v-model="realEstate.BlockModel.RoomNoMustSelect"></Checkbox>
                                                    <span>房源【楼栋座】必须从已有楼栋座中选取</span>
                                                </h4>

                                            </li>
                                            <li class="inputListLong checkList">
                                                <h3>房号必选</h3>
                                                <h4 class="colorS">
                                                    <Checkbox v-model="realEstate.BlockModel.BuildingMustSelect"></Checkbox>
                                                    <span>房源【房号】必须从已有房号中选取（所有楼栋座必须以生成房号）</span>
                                                </h4>

                                            </li>
                                        </ul>
                                        -->
                                         
                                        <ul class="houseTab1 clearfix"  v-show="show" >
                                            <li class="inputList">
                                                <h3>建筑面积</h3>
                                                <input type="text" v-model="realEstate.BuildingAreaAmount"  />

                                                <h3>总户数</h3>
                                                <input type="text" v-model="realEstate.BlockModel.HouseholdAmount" />
                                            </li>
                                            <li class="inputList">
                                                <h3>建筑结构</h3>
                                                <input type="text" v-model="realEstate.BlockModel.ConstructionType" />

                                                <h3>外墙</h3>
                                                <input type="text" v-model="realEstate.BlockModel.OuterWall" />
                                            </li>
                                            <li class="inputList">
                                                <h3>土地</h3>
                                                <input type="text" v-model="realEstate.BlockModel.Land" />

                                                <h3>地号</h3>
                                                <input type="text" v-model="realEstate.BlockModel.LandNo" />
                                            </li>
                                            <li class="inputList">
                                                <h3>物业公司</h3>
                                                <input type="text" v-model="realEstate.BlockModel.PropertyCompanyName" />

                                                <h3>物业电话</h3>
                                                <input type="text" v-model="realEstate.BlockModel.PropertyCompanyPhone" />
                                            </li>
                                            <li class="inputListLong">
                                                <h3>英文名称</h3>
                                                <input type="text" v-model="realEstate.BlockModel.EnglishName" />
                                            </li>
                                            <li class="inputListLong">
                                                <h3>英文地址</h3>
                                                <input type="text" v-model="realEstate.BlockModel.EnglishAddress" />
                                            </li>
                                        </ul>
                                        <p class="hot-moreBtn" v-on:click="toggleMore">
                                            <span>{{show?"隐藏更多小区资料":"显示更多小区资料"}}</span>
                                        </p>

                                       
                                    </div>
                                </div>`;
    var initBlockInfo = {
            "BlockModel": {
                "BuildingMustSelect": false,
                "RoomNoMustSelect": false,
                "PropertyFee":0,
                "AveragePrice": {
                    "Price": {
                        "Amount":0,
                        "CurrencyName": "\uffe5",
                        "CurrencyValue": 1
                    },
                    "UnitName": "",
                    "UnitValue": 0
                },
                "Alias": "",
                "EnglishName": "",
                "Pinyin": "",
                "EnglishAddress": "",
                "PropertyCompanyName":"",
                "PropertyCompanyPhone":"",
                "PropertyUsageValue": 0,
                "PropertyUsageName": "",
                "LandNo": "",
                "Land": "",
                "OuterWall": "",
                "ConstructionType": "",
                "HouseholdAmount": 0,
                "BuiltTime": (new Date()).getFullYear() + "-" + ((new Date()).getMonth() + 1) + "-" + (new Date()).getDate(),
                "BuildingAmount": 0,
                "SubmitTime": (new Date()).getFullYear() + "-" + ((new Date()).getMonth() + 2) + "-" + (new Date()).getDate(),
                "Id": "",
                "SortNo": 0,
                "Name": "",
                "ParentNodeName": "",
                "ParentNodeId": "",
                "ChildrenNode": []
            },
            "BuildingAreaAmount":0,
            "BuildingAreaUnitValue":0,


            "Id": "",
            "Name": "",
            "Pinyin": "",
            "Memo": null,
            "OrganizationNodeId": null,
            "CustomAreaId": null,
            "CustomAreaName": null,
            "AddressAdministrativeDivisionCode": "",
            "AddressDetail": "",
            "AddressSummary": "",
            "MapArea": null,
            "PropertyTypeValue": 0,
            "PropertyTypeName": "Office",
            "CreatedTime": ""
    };

    var BlockDetail = {
        name: "block-detail",
        template: template,
        methods: {
            submittime_changed: function (selectedDate) {

                 this.realEstate.BlockModel.SubmitTime=selectedDate;
                 // this.$emit("update:testdate",selectedSubmitTime);
            },
            builttime_changed: function (selectedDate) {
                this.realEstate.BlockModel.BuiltTime=selectedDate;
                // this.$emit("update:realEstate.BuiltTime",selectedBuiltTime);
            },
            division_changed: function (selectedDivision) {
                this.realEstate.DivisionCode = selectedDivision.length === 0 ? ""
                    : selectedDivision[selectedDivision.length - 1];
            },
            toggleMore: function () {
                this.show = !this.show;
            },
            getParent: function (data, currentVal) {

                var getParent = function (data, currentVal) {
                    var res = {
                        data: [],
                        flag: false
                    };
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].value === currentVal) {
                            res.flag = true;
                            res.data.unshift(data[i].value);
                            break;
                        } else if (data[i].hasOwnProperty('children')) {
                            var nextRes = getParent(data[i].children, currentVal);
                            if (nextRes.flag) {
                                res.flag = true;
                                res.data = res.data.concat(nextRes.data);
                                res.data.unshift(data[i].value);
                                break;
                            }
                        }
                    }

                    return res;
                }

                return getParent(data, currentVal).data;
            },



        },
        data: function () {
            return {

                realEstate:JSON.parse(JSON.stringify(initBlockInfo)),

                show: false,
                //添加楼栋

                customArea: [],

                /*省份城市选择*/
                cityData: AdministrativeDivisionOption,

                //物业类型

                propertyUsage: [
                    {
                        value: 0,
                        label: '商用'
                    },
                    {
                        value: 1,
                        label: '民用'
                    },

                ],

                //物业用途

                propertyType: [
                    {
                        value: 0,
                        label: '写字楼'
                    },
                    {
                        value: 1,
                        label: '住宅'
                    },

                ]

            }


        },
        created:function(){

            var that=this;
            window.bus.$on("editblock", function (block) {

                if (block == null) {
                    that.realEstate=JSON.parse(JSON.stringify(initBlockInfo));
                    return;
                }
               Object.assign(that.realEstate, block);

                //  that.blockinfo.realEstate=block;
                var divisionCode=that.getParent(AdministrativeDivisionOption,block.AddressAdministrativeDivisionCode);
                that.realEstate.AddressAdministrativeDivisionCode=divisionCode;
            });

            window.bus.$on("saveblock",function() {
                that.realEstate.OrganizationNodeId = localStorage.companyId;
                    var divisionCode = that.realEstate.AddressAdministrativeDivisionCode;
                that.realEstate.AddressAdministrativeDivisionCode = divisionCode[divisionCode.length - 1];
                     api.SavePlot(that.realEstate)
                        .done(function (r) {
                            if (r.IsSuccess) {
                                that.$Message.info("Success");
                                window.bus.$emit("block_save_success", r.ResultObject);
                            }
                            else {
                                that.$Message.error({content: r.ErrMsg, duration: 3000, closable: true});
                            }
                        })
                        .fail(function (err) {
                            that.$Message.error({content: err, duration: 3000, closable: true});
                        });
                }
            );

            api.getCustomArea({companyId: localStorage.companyId}).done(function (data) {
                if (data.IsSuccess) {
                    for (var i in data.ResultObject) {
                        that.customArea.push({label: data.ResultObject[i].Name, value: data.ResultObject[i].Id})
                    }

                }
                else {
                    that.$Message.info("所属区域有误:" + data.ErrMessage)
                }

            });

        }


    }
    return BlockDetail
})
